﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// Token: 0x02000557 RID: 1367
public class UI_DragWindow : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerUpHandler, IEventSystemHandler
{
	// Token: 0x060022E0 RID: 8928 RVA: 0x000D2DB4 File Offset: 0x000D0FB4
	private void Start()
	{
		if (null == this.rtMove)
		{
			this.rtMove = (base.transform as RectTransform);
		}
		if (null == this.rtDrag)
		{
			this.rtDrag = this.rtMove;
		}
		if (null == this.rtCanvas)
		{
			this.SearchCanvas();
		}
		if (null != this.rtCanvas && null == this.canvas)
		{
			this.canvas = this.rtCanvas.GetComponent<Canvas>();
			if (this.canvas)
			{
				this.cscaler = this.rtCanvas.GetComponent<CanvasScaler>();
			}
		}
	}

	// Token: 0x060022E1 RID: 8929 RVA: 0x000D2E6C File Offset: 0x000D106C
	private void SearchCanvas()
	{
		GameObject gameObject = base.gameObject;
		for (;;)
		{
			this.canvas = gameObject.GetComponent<Canvas>();
			if (this.canvas)
			{
				break;
			}
			if (null == gameObject.transform.parent)
			{
				return;
			}
			gameObject = gameObject.transform.parent.gameObject;
		}
		this.rtCanvas = (gameObject.transform as RectTransform);
	}

	// Token: 0x060022E2 RID: 8930 RVA: 0x000D2EE4 File Offset: 0x000D10E4
	private float GetScreenRate()
	{
		float num = (float)Screen.width;
		float num2 = (float)Screen.height;
		Vector2 one = Vector2.one;
		one.x = num / this.cscaler.referenceResolution.x;
		one.y = num2 / this.cscaler.referenceResolution.y;
		return one.x * (1f - this.cscaler.matchWidthOrHeight) + one.y * this.cscaler.matchWidthOrHeight;
	}

	// Token: 0x060022E3 RID: 8931 RVA: 0x000D2F6C File Offset: 0x000D116C
	private void CalcDragPosOverlay(PointerEventData ped)
	{
		Vector2 anchoredPosition = ped.position - this.dragStartPosBackup;
		anchoredPosition.x /= this.rtCanvas.localScale.x;
		anchoredPosition.y /= this.rtCanvas.localScale.y;
		float num = (this.rtDrag.rect.size.x != this.rtDrag.sizeDelta.x) ? (this.rtDrag.rect.size.x - this.rtDrag.sizeDelta.x) : this.rtDrag.sizeDelta.x;
		float num2 = (this.rtDrag.rect.size.y != this.rtDrag.sizeDelta.y) ? (this.rtDrag.rect.size.y - this.rtDrag.sizeDelta.y) : this.rtDrag.sizeDelta.y;
		anchoredPosition.x = Mathf.Clamp(anchoredPosition.x, 0f, (float)Screen.width / this.rtCanvas.localScale.x - num);
		anchoredPosition.y = -Mathf.Clamp(-anchoredPosition.y, 0f, (float)Screen.height / this.rtCanvas.localScale.y - num2);
		float num3 = (this.rtMove.rect.size.x != this.rtMove.sizeDelta.x) ? (this.rtMove.rect.size.x - this.rtMove.sizeDelta.x) : this.rtMove.sizeDelta.x;
		float num4 = (this.rtMove.rect.size.y != this.rtMove.sizeDelta.y) ? (this.rtMove.rect.size.y - this.rtMove.sizeDelta.y) : this.rtMove.sizeDelta.y;
		anchoredPosition.x += num3 * this.rtMove.pivot.x;
		anchoredPosition.y += num4 * (this.rtMove.pivot.y - 1f);
		this.rtMove.anchoredPosition = anchoredPosition;
	}

	// Token: 0x060022E4 RID: 8932 RVA: 0x000D32A8 File Offset: 0x000D14A8
	private void CalcDragPosScreenSpace(PointerEventData ped)
	{
		Vector2 anchoredPosition = ped.position - this.dragStartPosBackup;
		float screenRate = this.GetScreenRate();
		anchoredPosition.x /= screenRate;
		anchoredPosition.y /= screenRate;
		float num = (this.rtDrag.rect.size.x != this.rtDrag.sizeDelta.x) ? (this.rtDrag.rect.size.x - this.rtDrag.sizeDelta.x) : this.rtDrag.sizeDelta.x;
		float num2 = (this.rtDrag.rect.size.y != this.rtDrag.sizeDelta.y) ? (this.rtDrag.rect.size.y - this.rtDrag.sizeDelta.y) : this.rtDrag.sizeDelta.y;
		anchoredPosition.x = Mathf.Clamp(anchoredPosition.x, 0f, (float)Screen.width / screenRate - num);
		anchoredPosition.y = -Mathf.Clamp(-anchoredPosition.y, 0f, (float)Screen.height / screenRate - num2);
		float num3 = (this.rtMove.rect.size.x != this.rtMove.sizeDelta.x) ? (this.rtMove.rect.size.x - this.rtMove.sizeDelta.x) : this.rtMove.sizeDelta.x;
		float num4 = (this.rtMove.rect.size.y != this.rtMove.sizeDelta.y) ? (this.rtMove.rect.size.y - this.rtMove.sizeDelta.y) : this.rtMove.sizeDelta.y;
		anchoredPosition.x += num3 * this.rtMove.pivot.x;
		anchoredPosition.y += num4 * (this.rtMove.pivot.y - 1f);
		this.rtMove.anchoredPosition = anchoredPosition;
	}

	// Token: 0x060022E5 RID: 8933 RVA: 0x000D35A0 File Offset: 0x000D17A0
	private void SetClickPosOverlay(PointerEventData ped)
	{
		Vector2 zero = Vector2.zero;
		float num = (this.rtMove.rect.size.x != this.rtMove.sizeDelta.x) ? (this.rtMove.rect.size.x - this.rtMove.sizeDelta.x) : this.rtMove.sizeDelta.x;
		float num2 = (this.rtMove.rect.size.y != this.rtMove.sizeDelta.y) ? (this.rtMove.rect.size.y - this.rtMove.sizeDelta.y) : this.rtMove.sizeDelta.y;
		zero.x = (this.rtMove.anchoredPosition.x - num * this.rtMove.pivot.x) * this.rtCanvas.localScale.x;
		zero.y = (this.rtMove.anchoredPosition.y - num2 * (this.rtMove.pivot.y - 1f)) * this.rtCanvas.localScale.y;
		this.dragStartPosBackup = ped.position - zero;
	}

	// Token: 0x060022E6 RID: 8934 RVA: 0x000D3760 File Offset: 0x000D1960
	private void SetClickPosScreenSpace(PointerEventData ped)
	{
		float screenRate = this.GetScreenRate();
		Vector2 zero = Vector2.zero;
		float num = (this.rtMove.rect.size.x != this.rtMove.sizeDelta.x) ? (this.rtMove.rect.size.x - this.rtMove.sizeDelta.x) : this.rtMove.sizeDelta.x;
		float num2 = (this.rtMove.rect.size.y != this.rtMove.sizeDelta.y) ? (this.rtMove.rect.size.y - this.rtMove.sizeDelta.y) : this.rtMove.sizeDelta.y;
		zero.x = (this.rtMove.anchoredPosition.x - num * this.rtMove.pivot.x) * screenRate;
		zero.y = (this.rtMove.anchoredPosition.y - num2 * (this.rtMove.pivot.y - 1f)) * screenRate;
		this.dragStartPosBackup = ped.position - zero;
	}

	// Token: 0x060022E7 RID: 8935 RVA: 0x000D3900 File Offset: 0x000D1B00
	public void OnPointerDown(PointerEventData ped)
	{
		switch (this.canvas.renderMode)
		{
		case RenderMode.ScreenSpaceOverlay:
			this.SetClickPosOverlay(ped);
			break;
		case RenderMode.ScreenSpaceCamera:
			this.SetClickPosScreenSpace(ped);
			break;
		}
	}

	// Token: 0x060022E8 RID: 8936 RVA: 0x000D3954 File Offset: 0x000D1B54
	public void OnBeginDrag(PointerEventData ped)
	{
		switch (this.canvas.renderMode)
		{
		case RenderMode.ScreenSpaceOverlay:
			this.CalcDragPosOverlay(ped);
			break;
		case RenderMode.ScreenSpaceCamera:
			this.CalcDragPosScreenSpace(ped);
			break;
		}
	}

	// Token: 0x060022E9 RID: 8937 RVA: 0x000D39A8 File Offset: 0x000D1BA8
	public void OnDrag(PointerEventData ped)
	{
		switch (this.canvas.renderMode)
		{
		case RenderMode.ScreenSpaceOverlay:
			this.CalcDragPosOverlay(ped);
			break;
		case RenderMode.ScreenSpaceCamera:
			this.CalcDragPosScreenSpace(ped);
			break;
		}
	}

	// Token: 0x060022EA RID: 8938 RVA: 0x000D39FC File Offset: 0x000D1BFC
	public void OnEndDrag(PointerEventData ped)
	{
	}

	// Token: 0x060022EB RID: 8939 RVA: 0x000D3A00 File Offset: 0x000D1C00
	public void OnPointerUp(PointerEventData ped)
	{
	}

	// Token: 0x040023F7 RID: 9207
	public RectTransform rtDrag;

	// Token: 0x040023F8 RID: 9208
	public RectTransform rtMove;

	// Token: 0x040023F9 RID: 9209
	public RectTransform rtCanvas;

	// Token: 0x040023FA RID: 9210
	private Canvas canvas;

	// Token: 0x040023FB RID: 9211
	private CanvasScaler cscaler;

	// Token: 0x040023FC RID: 9212
	private Vector2 dragStartPosBackup = Vector2.zero;
}
